package com.travel.service.user;

import com.travel.common.ServerResponse;
import com.travel.dao.user.TouristUserMapper;
import com.travel.pojo.WeiXin.WeiXinLogin;
import com.travel.pojo.user.Sign;
import com.travel.pojo.user.TouristUser;
import com.travel.service.WeiXin.WeiXinApi;
import com.travel.utils.xeixinDecrypt.DecryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


@Service
public class WeiXinUserServer {

    @Autowired
    WeiXinApi weiXinApi;

    @Autowired
    TouristUserMapper touristUserMapper;

    @Autowired
    DecryptUtil decryptUtil;

    /**
     * 选择微信的登录用户注册
     * openid是唯一用户唯一标识，通过微信登录返回的code再远程调用微信接口获取。
     * 二次登录的微信用户先用code换取openid,再使用openid查询数据库中是否有用户信息，
     * 有就直接返回查出来的信息，没有则是第一次微信登录，进行注册
     *
     * @param touristUser 微信用户信息
     *                    插入后返回用户id,返回给前端
     */
    public ServerResponse<TouristUser> wxtregister(TouristUser touristUser) throws Exception {
        Calendar calendar = new GregorianCalendar();
        switch (touristUser.getLogintype()) {
            case "APP":
                TouristUser touristUser1 = touristUserMapper.queryWxUserByunionid(touristUser.getUnionid());
                if (touristUser1 != null) {
                    return ServerResponse.createBySuccess("登录成功", touristUser1);
                } else {
                    touristUser.setBean_number(30);
                    touristUserMapper.insertWxTuser(touristUser);
                    Date date=new Date();
                    calendar.setTime(date);
                    calendar.add(calendar.DATE,-1);
                    date=calendar.getTime();
                    Sign sign=new Sign();
                    sign.setSigntime(date);
                    sign.setContinuity_count(0);
                    sign.setSum_count(0);
                    sign.setMark(0);
                    sign.setUserid(touristUser.getId());
                    touristUserMapper.addUserSign(sign);
                    //将返回的签到表id返回给前端
                    touristUser.setSignid(sign.getId());
                    //更新用户signid
                    touristUserMapper.updateUserSignIdById(touristUser.getId(),sign.getId());
                    return ServerResponse.createBySuccess("APP微信注册成功", touristUser);
                }

            default:
                WeiXinLogin weiXinLogin = weiXinApi.getOpenIdAndUnionId(touristUser.getCode());
             /*   System.out.println("微信登录小程序返回数据："+weiXinLogin);
                System.out.println("加密数据："+touristUser.getEncryptedData());
                System.out.println("iv:"+touristUser.getIv());
                System.out.println("session_key:"+weiXinLogin.getSession_key());
                System.out.println("开始解密");*/

                TouristUser touristUser2 = touristUserMapper.queryWxUserByopenid(weiXinLogin.getOpenid());
                if (touristUser2 != null){
                    return ServerResponse.createBySuccess("登录成功", touristUser2);
                }else {
                    touristUser.setOpenid(weiXinLogin.getOpenid());
                    touristUser.setSession_key(weiXinLogin.getSession_key());
                    touristUser.setBean_number(30);
                    touristUserMapper.insertWxTuser(touristUser);
                    Date date=new Date();
                    calendar.setTime(date);
                    calendar.add(calendar.DATE,-1);
                    date=calendar.getTime();
                    Sign sign=new Sign();
                    sign.setSigntime(date);
                    sign.setContinuity_count(0);
                    sign.setSum_count(0);
                    sign.setMark(0);
                    sign.setUserid(touristUser.getId());
                    touristUserMapper.addUserSign(sign);
                    //将返回的签到表id返回给前端
                    touristUser.setSignid(sign.getId());
                    //更新用户signid
                    touristUserMapper.updateUserSignIdById(touristUser.getId(),sign.getId());
                    return ServerResponse.createBySuccess("小程序微信注册成功", touristUser);
                }
        }
    }




    /**
     * 普通注册用户授权微信更新openid
     * @param id 用户唯一标识
     * @param code 微信登录api授权获取
     */
    public ServerResponse<String> setTouristUserOpenidById(int id,String code){
        WeiXinLogin weiXinLogin=weiXinApi.getOpenIdAndUnionId(code);
        touristUserMapper.updateUserOpenidById(id, weiXinLogin.getOpenid());
        return ServerResponse.createBySuccseeMessage(weiXinLogin.getOpenid());
    }
}
